#include <iostream>
using namespace std;
typedef long long LL;
const int NN = 1e5 + 4;
struct Node {
  int v, w, fa, l, r;  // v:搜索树的值, w:堆的权值
  bool operator<(const Node &b) { return v < b.v; }
  void init(int _v, int _w) { v = _v, w = _w, fa = l = r = 0; }
} A[NN];
void build(int n) {  // 建笛卡尔树，满足小根堆性质
  for (int i = 1, j; i <= n; i++) {
    Node &x = A[i];
    for (j = i - 1; A[j].w > x.w;) j = A[j].fa;
    x.l = A[j].r, A[x.l].fa = i;
    A[j].r = i, x.fa = j;
  }
}
LL dfs(int x, LL &ans) {  // 一次dfs更新答案就可以了
  if (!x) return 0;
  LL sz = dfs(A[x].l, ans) + dfs(A[x].r, ans) + 1;
  ans = max(ans, sz * A[x].w);
  return sz;
}
int main() {
  for (int n; scanf("%d", &n), n;) {
    A[0].init(0, 0);
    for (int i = 1, h; i <= n; i++) scanf("%d", &h), A[i].init(i, h);
    build(n);
    LL ans = 0;
    dfs(A[0].r, ans);
    printf("%lld\n", ans);
  }
  return 0;
}
